package com.yifeng.servlet.admin;

import com.yifeng.factory.ServiceFactory;
import com.yifeng.service.UserService;
import com.yifeng.servlet.BaseServlet;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(value = "/admin/user/*")
public class UserDeleteServlet extends BaseServlet {

    private final UserService userService = ServiceFactory.getUserService();

    @Override
    protected void doHandle(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String pathInfo = req.getPathInfo();
        if(pathInfo==null || pathInfo.trim().isEmpty() || !pathInfo.matches("/\\d+")){
            fail(resp,"新闻ID格式错误");
            return;
        }
        try {
            Integer id = Integer.parseInt(pathInfo.substring(1));
            var result = userService.deleteUserById(id);
            writeJson(resp, result);
        } catch (NumberFormatException e) {
            fail(resp, "参数错误");
        }
    }


}